//
//  MyBuyTogetherAllViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/12.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "MyBuyTogetherAllViewController.h"

#import "MyBuyTogetherTableViewCell.h"
#import "MyBuyTogetherModel.h"
#import "Share.h"

@interface MyBuyTogetherAllViewController ()<UITableViewDataSource, UITableViewDelegate, MyBuyTogetherCellDelegate>

@property (strong, nonatomic) UITableView *buyTogetherTableView;
@property (strong ,nonatomic) NSMutableArray *buyTogetherInfoArr;
@property NSInteger page;

@end

@implementation MyBuyTogetherAllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	[self createUI];
	[self getBuyTogetherList];
	[self refreshData];
    // Do any additional setup after loading the view.
}

- (void)createUI {
	self.navigationItem.title = @"我的拼单";
	_buyTogetherTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
	[_buyTogetherTableView registerNib:[UINib nibWithNibName:@"MyBuyTogetherTableViewCell" bundle:nil] forCellReuseIdentifier:@"buyTogetherCell"];
	_buyTogetherTableView.dataSource = self;
	_buyTogetherTableView.delegate = self;
	_buyTogetherTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	_buyTogetherTableView.backgroundColor = UISeparatedColorRGB;
	[self.view addSubview:_buyTogetherTableView];
}


#pragma mark ==========UITableView==========
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	MyBuyTogetherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"buyTogetherCell"];
	MyBuyTogetherModel *model = self.buyTogetherInfoArr[indexPath.row];
	cell.model = model;
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	cell.delegate = self;
	return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return self.buyTogetherInfoArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
	return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
}

- (void)refreshData {
	__weak typeof (self) weakSelf = self;
	
	_buyTogetherTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
		NSLog(@"下拉刷新");
		weakSelf.page = 1;
		[weakSelf getBuyTogetherList];
	}];
	
	_buyTogetherTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
		NSLog(@"上拉加载");
		weakSelf.page++;
		[weakSelf getBuyTogetherList];
	}];
	
}

- (void)getBuyTogetherList {
	NSDictionary *param = @{
							@"per_page":@"6",
							@"page":[NSString stringWithFormat:@"%ld", self.page],
							@"status":@"paid"
							};
	
	[[NetworkTool sharedTool] requestWithName:@"获取我的拼单列表" URLString:APIMyEcBuy parameters:param method:@"GET" callBack:^(id responseObject) {
		
		NSArray *arr = responseObject[@"data"];
		if (self.page == 1) {
			[self.buyTogetherInfoArr removeAllObjects];
		}
		for (NSDictionary *dict in arr) {
			
			MyBuyTogetherModel *model = [[MyBuyTogetherModel alloc] init];
			[model setValuesForKeysWithDictionary:dict];
			[self.buyTogetherInfoArr addObject:model];
		}
		[self.buyTogetherTableView reloadData];
		 dispatch_async(dispatch_get_main_queue(), ^{
		 [self.buyTogetherTableView reloadData];
		 [self.buyTogetherTableView.mj_header endRefreshing];
		 [self.buyTogetherTableView.mj_footer endRefreshing];
		 });
	}];
}

- (NSMutableArray *)buyTogetherInfoArr
{
	if (!_buyTogetherInfoArr) {
		_buyTogetherInfoArr = [[NSMutableArray alloc] init];
	}
	return _buyTogetherInfoArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)shareToFriend:(NSInteger)goodId :(NSString *)img :(NSString *)title
{
	NSLog(@"点击分享");
	NSString *urlStr = [NSString stringWithFormat:@"%@/mobile/download/index/goods_id/%ld", APIBackEndUrl, goodId];
	
	NSDictionary *param = @{@"text":@"我在星河汇发现一个很划算的拼单，快来看看吧",
							@"img":img,
							@"url":urlStr,
							@"title":title
							};
	[Share shareAction:param];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
